package com.ningkang.auth.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName:
 * @Description:
 * @Author: zhuwenqiang
 * @Date: 2024/6/22
 */
@Configuration
@ConfigurationProperties(prefix = "sms")
@Data
@Log4j2
public class InitSmsClient {

    /**
     * 阿里云主账号AccessKey，accessKeySecret拥有所有API的访问权限
     */
    private String accessKeyId;

    private String accessKeySecret;

    /**
     * 短信访问域名
     */
    private String endpoint;


    /**
     * 初始化短信对象
     *
     * @return
     * @throws Exception
     */
    @Bean
    public Client initClient() throws Exception {
        log.info("初始化短信服务开始");
        long startTime = System.currentTimeMillis();
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = endpoint;
        log.info("初始化短信服务结束：耗时：{}MS", (System.currentTimeMillis() - startTime));
        return new Client(config);
    }


}
